<!--
 * @Author: MrLee
 * @Date: 2024-08-24 08:55:14
 * @LastEditors: MrLee
 * @LastEditTime: 2024-09-14 09:27:39
 * @Description: 请填写简介
-->
<template>
  <custom-navbar :title="title" :auto-back="true" left-icon-color="#303133" />
  <up-gap height="30rpx" />
  <view class="flex items-center justify-end text-[26rpx] text-[#1f50f2] text-right px-24rpx">
    <view class="iconfont icon-jiahao mr-12rpx"></view>
    <view>新增</view>
  </view>
  <up-gap height="30rpx" />
  <view class="px-24rpx">
    <infocard :user-info-list="moreList" @actionTmp="oneAction" />
  </view>
  <view class="flex fixed bottom-0 h-178rpx w-[100%] bg-#fff shadow-[0px_0px_12px_0px_rgba(238,239,241,0.50)]">
    <view
      class="mx-auto my-0 mt-[24rpx] h-82rpx w-[40%] rounded-[42rpx] bg-#f7f7f8 text-center text-[30rpx] text-[#1d2129] font-medium leading-[82rpx]"
      @click="deleteItem"
    >
      删除
    </view>
    <view
      class="mx-auto my-0 mt-[24rpx] h-82rpx w-[40%] rounded-[42rpx] bg-#1f50f2 text-center text-[30rpx] text-white font-medium leading-[82rpx]"
      @click="submitValue()">
      添加
    </view>
  </view>
  <up-picker :show="show" :columns="columns" @confirm="confirm" @cancel="show = false"></up-picker>
</template>

<script setup lang="ts">
  import infocard from '@/pages/components/infocard/index.vue';
  import { useUserStore } from '@/store';
  import {
    userJobType,
    addUserIntention,
    getIntentionDetail,
    getIntentionDetailUpdate,
    getIntentionDetailDel
  } from '@/api/user/index'

  let moreList = ref(null)
  let title = ref('')
  const moreListUser = reactive([
    {
      title: '类型',
      value: 0,
      label: '全职',
      valueType: 'myRadio',
      radioOptions: [
        {
          label: '全职',
          value: 0
        },
        {
          label: '实习',
          value: 1
        }
      ]
    },
    {
      title: '期望城市',
      value: '',
      valueType: 'city',
      rigthIco: true,
    },
    {
      title: '期望职位',
      value: '请选择',
      label: '请选择',
      rigthIco: true,
      valueType: 'select',
    },
    {
      title: '期望薪资',
      value: '请选择',
      label: '请选择',
      rigthIco: true,
      valueType: 'select',
      selectOptions: []
    },
  ]);
  const moreListEnterprise = reactive([
    {
      title: '岗位选择',
      value: '请选择',
      valueType: 'text',
      rigthIco: true,
      options: [["北京", "大连", "上海"]],
    },
    {
      title: '期望学历',
      value: '请选择',
      rigthIco: true,
      valueType: 'text',
      options: [["老板", "秘书", "经理"]],
    },
    {
      title: '期望学校',
      value: '请选择',
      rigthIco: true,
      valueType: 'text',
      options: [["10-20k", "100-200k", "1-2k"]],
    },
    {
      title: '期望专业',
      value: '请选择',
      rigthIco: true,
      valueType: 'text',
      options: [["10-20k", "100-200k", "1-2k"]],
    },
    {
      title: '期望籍贯',
      value: '请选择',
      rigthIco: true,
      valueType: 'text',
      options: [["10-20k", "100-200k", "1-2k"]],
    }
  ]);
  let columns = reactive([]);
  let show = ref(false)
  const oneAction = (params) => {
    if (params.options) {
      show.value = true;
      columns = params.options
    }
  }

  type ClientType = "customer" | "enterprise";
  const { clientType } = useUserStore();

  moreList.value = clientType == "customer" ? moreListUser : moreListEnterprise;
  title.value =
    clientType == "customer" ? '添加求职偏好' : '添加招聘偏好';

  const confirm = (v) => {
    console.log(v)
  };

  const submitValue = async () => {
    console.log(moreListUser, '列表信息')
    if (!moreListUser) {
      uni.showToast({
        title: '请返回重新加载页面',
        icon: 'none'
      })
      return
    }
    if (moreListUser[2].value == "请选择") {
      uni.showToast({
        title: '请选择期望职位',
        icon: none
      })
      return
    }
    if (moreListUser[3].value == "请选择") {
      uni.showToast({
        title: '请选择期望薪资',
        icon: 'none'
      })
      return
    }

    let params = {
      id: copyItem ? '' : itemId,
      nature: moreListUser[0].label,
      province_id: moreListUser[1].value.split(',')[0],
      city_id: moreListUser[1].value.split(',')[1],
      position_id: moreListUser[2].value,
      salary_min: moreListUser[3].value.split('-')[0],
      salary_max: moreListUser[3].value.split('-')[1] ? moreListUser[3].value.split('-')[1] : moreListUser[3].value.split('-')[0]
    }
    if(params.id) {
      let r = await getIntentionDetailUpdate(params)
    }else {
      let r = await addUserIntention(params)
    }
    uni.navigateBack()
    // uni.redirectTo({
    //   url: '/pages/tab/home/more/preference'
    // })
  }
  //删除
  const deleteItem = () => {
    console.log('点击了')
    uni.showModal({
      title: '提示',
      content: '确定删除该条数据？',
      success: async (res) => {
        if (res.confirm) {
          let params = {
            id: itemId
          }
          let r = await getIntentionDetailDel(params)
          uni.navigateBack()
        }
      }
    })
  }
  const getType = () => {
    userJobType().then(res => {
      if (res) {
        moreListUser[2].selectOptions = []
        res.map(item => {
          if (item.level === 2) {
            console.log(item)
            moreListUser[2].selectOptions.push(
              {
                label: item.content,
                value: item.id
              }
            )
          }
        })
        console.log(moreListUser[2].selectOptions)
      }
    })
  }
  // 获取详情
  const getDetails = () => {
    getIntentionDetail({id: itemId}).then(res => {
      if(res) {
        moreListUser[0].value = res.nature === '全职' ? 0 : 1
        moreListUser[0].label = res.nature
        moreListUser[1].value = res.province_id + ',' + res.city_id
        moreListUser[1].label = res.province.name + ' ' + res.city.name
        moreListUser[2].value = res.position.id
        moreListUser[2].label = res.position.content
        moreListUser[3].value = res.salary_min + '-' + res.salary_max
        moreListUser[3].label = res.salary_min + '-' + res.salary_max
      }
    })
  }
  let itemId = reactive('')
  let copyItem = reactive('')
  onLoad((params) => {
    if(params.id) {
      itemId = params.id
      copyItem = params.type
      getDetails()
    }
    console.log('------', params)
  })
  onMounted(() => {
    getType()
    let data = JSON.parse(uni.getStorageSync('config')).configCli
    data.config_salary$$薪资配置.map((item, index) => {
      if (index >= (data.config_salary$$薪资配置.length - 1)) {
        moreListUser[3].selectOptions.push({
          label: item.value + '+',
          value: item.value
        })
      } else {
        console.log(data.config_salary$$薪资配置[index + 1], index)
        moreListUser[3].selectOptions.push({
          label: item.value + '-' + data.config_salary$$薪资配置[index + 1].value,
          value: item.value + '-' + data.config_salary$$薪资配置[index + 1].value
        })
      }
    })
  })
</script>
